package com.yang.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yang.model.BaseResult;
import com.yang.model.PageResult;
import com.yang.model.Role;
import com.yang.model.User;
import com.yang.service.RoleService;
import com.yang.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("user")
@Api("swaggerDemoController相关的api")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	    @ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
	    @ApiImplicitParam(name = "id", value = "学生ID", paramType = "query", required = true, dataType = "string")
	    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
	    @ResponseBody
	  public Map<String, String> tets(@PathVariable String id) {
		
		Map<String, String> map=new HashMap<>();
		
		map.put(id+"", "张三");
		
		return map;
	}
	   
	    
	   
	    
	    
	    @RequestMapping("user")
	    public String toUser() {	    	
	    	return "user/user";
	    }
	    
	    @RequestMapping("queryTableData")
	    @ResponseBody
	    public BaseResult<Object> queryTableData( Integer currenrPage,Integer pageSize,String search_value) {			
	    	BaseResult<Object> result =new BaseResult<>();
	    	Map<String, Object> map =new HashMap<>();
	    	map.put("currenrPage", (currenrPage-1)*pageSize);
	    	map.put("pageSize", pageSize);
	    	map.put("search_value", search_value);
	    	List<User> users = userService.queryUsers(map);
	    	List<User> allUsers = userService.queryAllUsers(map);
	    	
	    	PageResult<User> pageResult=new PageResult<>();
	    	pageResult.setCurrentPage(currenrPage);
	    	pageResult.setPageSize(pageSize);
	    	pageResult.setPageTotal(allUsers.size()%pageSize==0?allUsers.size()/pageSize:allUsers.size()/pageSize+1);
	    	pageResult.setData(users);
	    	
	    	if (users !=null) {				
	    		result.setSuccess(true);
	    		result.setData(pageResult);
			}else {
				result.setSuccess(false);
			}
	    	 	    	
	    	return result;
		}
	    
	    
	    @RequestMapping("add")
	    public String toAdd() {
	    	
	    	return "user/add";
	    }
	    
	    
	    @RequestMapping("logout")
	    public String logout(HttpSession session ) {
	    	session.invalidate();
	    	return "redirect:login";
	    }
	    
	    
	    @RequestMapping("addUser")
	    @ResponseBody
	   public Object  addUser(User user) {
	    	BaseResult<Object> result=new BaseResult<>();
	       try {
	    	   user.setLoginpwd("123456");
	    	   SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	   user.setCreatetime(simpleDateFormat.format(new Date()));
	    	   userService.addUser(user);
	    	   result.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
		}	    	
	    	return result;
	    }
	    
	    @RequestMapping("edit")
	    public String edit(Integer userId,Model model) {
		User user=	userService.selectUserByUserId(userId);
		model.addAttribute("edituser", user);
	    	return "user/edit";
		}
	    @RequestMapping("updateUser")
	    @ResponseBody
	   public Object updateUser(User user) {
		BaseResult<Object> result =new BaseResult<>();
	    	try {
	    		userService.updateUser(user);
				result.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
	    	return result ;
	   }
	    @RequestMapping("deleteUser")
	    @ResponseBody
	    public Object deleteUser(Integer userId) {
	    	
	    	BaseResult<Object> result =new BaseResult<>();	    	
	    	try {
				
	    		userService.deleteUser(userId);
	    		result.setSuccess(true);
	    		
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
	    	
	       return	result ;
	    }
	    
	    
	    @RequestMapping("deleteUsers")
	    @ResponseBody
	    public Object deleteUsers(Integer[] userIds) {
	    	
	    	BaseResult<Object> result =new BaseResult<>();
	    	userService.deleteUsers(userIds);
	    	  result.setSuccess(true);
	    	try {
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
	    	
	    	
	    	return result;
	    }
	    
	    
	    @RequestMapping("assignRole")
	    public String assignRole(Integer userId,Model model) {
	    	
	    	User user = userService.selectUserByUserId(userId);
	    	model.addAttribute("user", user);
	    	
	    	List<Role> roles=roleService.getAllRoles();	    	
	    	List<Integer> crRoles=roleService.getCurrentRoles(userId);
	    	
	    	List<Role> assignRoleList =new ArrayList<>();
	    	List<Role> unassignRoleList =new ArrayList<>();
	    	
	    	for (Role role : roles) {
				if (crRoles.contains(role.getId())) {
					assignRoleList.add(role);
				}else {
					unassignRoleList.add(role);
				}
			}	    	
	    	model.addAttribute("assignRoleList", assignRoleList);
	    	model.addAttribute("unassignRoleList", unassignRoleList);
	    	return "user/assignRole";
	    }
	    
	    @RequestMapping("addRoles")
	    @ResponseBody
	    public Object addRoles(Integer userId,Integer[] unassignRoleList) {	    	
	    	BaseResult<Object> result =new BaseResult<>();	  
	    	Map<String, Object> param =new HashMap<>();
	    	try {
	    		param.put("userId", userId);
	    		param.put("roleids", unassignRoleList);
	    		userService.InsertRoles(param);
	    		result.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
	    	
	    	return result;
	    	
	    }
	    
	    @RequestMapping("reduceRoles")
	    @ResponseBody
	    public Object reduceRoles(Integer userId,Integer[] assignRoleList) {
	    	
	    	BaseResult<Object> result =new BaseResult<>();
	    	
	      	Map<String, Object> param =new HashMap<>();
	    	try {
	    		param.put("userId", userId);
	    		param.put("roleids", assignRoleList);
	    		userService.deleteRoles(param);
	    		result.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
	    	
	    	return result;
	    	
	    }

}
